package com.dream.cgomall.controller;
import com.dream.cgomall.DO.OrderDo;
import com.dream.cgomall.dto.ShoppingDTO;
import com.dream.cgomall.entity.CgoAdminAddress;
import com.dream.cgomall.entity.CgoOrder;
import com.dream.cgomall.entity.CgoOrderGoods;
import com.dream.cgomall.service.CgoAdminAddressService;
import com.dream.cgomall.service.impl.CgoOrderServiceImpl;
import com.dream.cgomall.vo.OrderVo;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

@RestController
@RequestMapping("/order")
public class CgoOrderController {
    @Autowired
    private CgoAdminAddressService cgoAdminAddressService;
    @Autowired
    private CgoOrderServiceImpl orderService;
    //根据用户id查询用户的默认收货地址
    @RequestMapping("/add/{userid}")
    public CgoAdminAddress findDefAddress(@PathVariable("userid") Integer userid){
        return cgoAdminAddressService.findDefAddress(userid);
    }
    //根据用户id查询用户的默认收货地址
    @RequestMapping("/addById/{addId}")
    public CgoAdminAddress findAddress(@PathVariable("addId") Integer addId){
        return cgoAdminAddressService.findAddressById(addId);
    }
    //
    @RequestMapping("/findshopping/{listId}")
    public List<ShoppingDTO> find(@PathVariable("listId") List<Integer> listId){
        return orderService.findShoppings(listId);
    }

    @RequestMapping("/insertorder")
    public Long insert(@RequestBody OrderDo orderDo){
        Long i = 0L;
        try {
          i = orderService.addToOrder(orderDo);
        } catch (Exception e){
            e.printStackTrace();
            return 0L;
        }
        return i;
    }
    @RequestMapping("/updateOrder/{id}")
    public boolean updateOrder(@PathVariable("id") Long id){
        int i = orderService.updateOrderStatus(id);
        if (i == 1){
            return true;
        }else {
            return false;
        }
    }
    @RequestMapping("/status/{statusId}/{userId}")
    public List<OrderVo> findStatus(@PathVariable("statusId") Integer statusId, @PathVariable("userId") Integer userId){
        List<OrderVo> allOrder = new ArrayList<>();
        try {
          allOrder = orderService.findAllOrder(statusId, userId);
        }catch (Exception e){
            System.out.println("记录异常");
        }
        return allOrder;
    }
    @RequestMapping("/findorder/{id}")
    public CgoOrder findorder(@PathVariable("id") Integer id){
       return orderService.finOrder(id);
    }
    @RequestMapping("/findshoop/{id}")
    public List<CgoOrderGoods> findShoop(@PathVariable("id") Integer id){
        Long i = id.longValue();
        return orderService.findshoopDetails(i);
    }
}
